package com.cw1.principle.inversion;

public class DependencyPass {
    public static void main(String[] args) {
        // 测试方式一
        ChangHong changHong = new ChangHong();
        OpenAndClose openAndClose = new OpenAndClose();
        openAndClose.open(changHong);

        System.out.println("====================");
        XiaoMi xiaoMi = new XiaoMi();
        OpenAndClose2 openAndClose2 = new OpenAndClose2(xiaoMi);
        openAndClose2.open();

        System.out.println("-===================");
        HuaWei huaWei = new HuaWei();
        OpenAndClose3 openAndClose3 = new OpenAndClose3();
        openAndClose3.setTv(huaWei);
        openAndClose3.open();
    }
}

/**
 * 方式一：通过接口实现依赖
 */
interface IOpenAndClose{
    void open(ITv iTv);
}
interface ITv{
    void play();
}
class OpenAndClose implements IOpenAndClose{
    public void open(ITv iTv) {
        iTv.play();
    }
}
class ChangHong implements ITv{
    public void play() {
        System.out.println("打开长虹电视机：牛皮");
    }
}

/**
 * 方式二：通过构造方法依赖传递
 */
interface IOpenAndClose2 {
    void open();
}
interface ITv1{
    void play();
}
class OpenAndClose2 implements IOpenAndClose2{
    private ITv1 tv;
    public void open() {
        this.tv.play();
    }
    // 通过构造器来把传参赋值给类的内部变量
    public OpenAndClose2(ITv1 iTv1){
        this.tv = iTv1;
    }
}

class XiaoMi implements ITv1{
    public void play() {
        System.out.println("打开小米电视机:流批2");
    }
}

/**
 * 方式三：通过setter方法传递
 * 通过构造方法初始化成员变量，成员变量执行方法
 */
interface IOpenAndClose3{
    void open();
    void setTv(ITv3 iTv3);
}
interface ITv3{
    void play();
}
class OpenAndClose3 implements IOpenAndClose3{
    private ITv3 iTv3;
    public void open() {
        this.iTv3.play();
    }
    public void setTv(ITv3 iTv3) {
        this.iTv3 = iTv3;
    }
}
class HuaWei implements ITv3{
    public void play() {
        System.out.println("打开华为电视机：流批3");
    }
}